#include <QDebug>
#include <QGridLayout>
#include <QLabel>
#include <QCheckBox>
#include <QSpacerItem>
#include "LexerEdit.h"
#include "Include/ITheme.h"
#include "Include/ILexers.h"
#include "ColorChooser.h"



LexerEdit::LexerEdit(Lexers::ILexer *lex, ITheme * theme, QWidget *parent) :
    QWidget(parent),
    _lex(lex),
    _theme(theme)
{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
    QGridLayout * lay = new QGridLayout(this);

    lay->addWidget(new HeaderLabel(trUtf8("Name"), this), 0, 0);
    lay->addWidget(new HeaderLabel(trUtf8("Bold"), this), 0, 1);
    lay->addWidget(new HeaderLabel(trUtf8("Italic"), this), 0, 2);
    lay->addWidget(new HeaderLabel(trUtf8("Color"), this), 0, 3);

    int row = 1;
    _theme->setLexer(_lex->name());
    foreach(int style, _lex->styles()){
        QLabel *lab = new QLabel(_lex->description(style), this);
        lay->addWidget(lab, row, 0);

        QCheckBox *bold = new QCheckBox(this);
        bold->setObjectName(_lex->nameInTheme(style)+"_bold");
        bold->setChecked(_theme->bold(_lex->nameInTheme(style)));
        lay->addWidget(bold, row, 1);

        QCheckBox *italic = new QCheckBox(this);
        italic->setObjectName(_lex->nameInTheme(style)+"_italic");
        italic->setChecked(_theme->italic(_lex->nameInTheme(style)));
        lay->addWidget(italic, row, 2);

        ColorChooser *fore = new ColorChooser(this);
        fore->setMinimumSize(100, 30);
        fore->setObjectName(_lex->nameInTheme(style)+"_fore");
        fore->setColor(_theme->foreground(_lex->nameInTheme(style), _lex->color(style)));
        lay->addWidget(fore, row, 3);

        row++;
    }
    lay->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding), row, 0);
    lay->setColumnStretch(0, 100);
    setLayout(lay);
}

void LexerEdit::save()
{
    _theme->setLexer(_lex->name());
    foreach(int style, _lex->styles()){
        QCheckBox *bold = findChild<QCheckBox *>(_lex->nameInTheme(style)+"_bold");
        _theme->setBold(_lex->nameInTheme(style), bold->isChecked());

        QCheckBox *italic = findChild<QCheckBox *>(_lex->nameInTheme(style)+"_italic");
        _theme->setItalic(_lex->nameInTheme(style), italic->isChecked());


        ColorChooser *fore = findChild<ColorChooser *>(_lex->nameInTheme(style)+"_fore");
        _theme->setForeground(_lex->nameInTheme(style), fore->color());
    }
}
